﻿using System;
using System.Collections.Generic;
using System.Text;

namespace csharpcore7_ch3_HideInheritedMembers
{

    public class A { public int Counter = 1; }
    public class B : A { public int Counter = 2; } //编译器会产一一个警告
    //故意隐藏一个成员,在子类的成员上使用new修饰符
    public class B1 : A { new public int Counter = 3; }

    //new和重写
    public class BaseClass
    {
        public virtual void Foo() { Console.WriteLine("BaseClass.Foo"); }
    }
    public class Override : BaseClass
    {
        public override void Foo()
        {
            Console.WriteLine("Override.Foo");
        }
    }

    public class Hider : BaseClass
    {
        public new void Foo()
        {
            Console.WriteLine("Hide.Foo");
        }
    }


    class Chapt3_2_5HideInheritedMembers
    {
        static void _Main()
        {
            //采用以下方法解决二义性
            A a;
            B b = new B();
            a = b;
            Console.WriteLine(a.Counter);//1
            Console.WriteLine(b.Counter);//2



            Override over = new Override();
            BaseClass b1 = over;
            over.Foo();
            b1.Foo();

            Hider h = new Hider();
            BaseClass b2 = h;
            h.Foo();
            b2.Foo();
        }
    }
}
